﻿using AutoMapper;
using MediatR;
using TraceabilityApp_Api.Application.Command.Slaughter_屠宰_;
using TraceabilityApp_Domain;
using TraceabilityApp_ErrorCode;
using TraceabilityApp_Infrastructure;

namespace TraceabilityApp_Api.Application.Handler.Slaughter_屠宰_
{
    public class AddSlaughterHandler : IRequestHandler<AddSlaughterCommand, ApiResult<int>>
    {
        private readonly IBase<SlaughterModel> slaughterRepo;
        private readonly IMapper mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="slaughterRepo"></param>
        /// <param name="mapper"></param>
        public AddSlaughterHandler(IBase<SlaughterModel> slaughterRepo,IMapper mapper)
        {
            this.slaughterRepo = slaughterRepo;
            this.mapper = mapper;
        }
        /// <summary>
        /// 添加屠宰信息--处理类
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<int>> Handle(AddSlaughterCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result=new ApiResult<int>();
            var slaughter = mapper.Map<SlaughterModel>(request);
            var r=slaughterRepo.Add(slaughter).Result;
            if(r>0)
            {
                result.Data= r;
                result.Code = ApiEnum.OK;
                result.Msg = "添加屠宰信息成功";
            }
            else
            {
                result.Data = r;
                result.Code = ApiEnum.Fail;
                result.Msg = "添加屠宰信息失败";
            }
            return Task.FromResult(result);
        }
    }
}
